<template>
  <div class="app-container">
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <router-link v-if="queryParams.maintenanceId != undefined" :to="'/property/company'" class="link-type">
          <el-button icon="el-icon-back" size="mini">
            返 回
          </el-button>
        </router-link>
      </el-col>
      <el-col :span="1.5">
        <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAdd"
                   v-hasPermi="['module:maintenanceContract:add']">新增</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="contractList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="所属物业" align="center" prop="propertyName" />-->
<!--      <el-table-column label="所属小区" align="center" prop="estateName" />-->
      <el-table-column label="合同标题" align="center" prop="contractName" />
<!--      <el-table-column label="合同编号" align="center" prop="contractNo" />-->
<!--      <el-table-column label="合同类型：" align="center" prop="contractType" />-->
<!--      <el-table-column label="合同文件路径：电子稿PDF" align="center" prop="filePath" />-->
      <el-table-column label="合同开始时间" align="center" prop="beginTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.serviceBeginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="合同结束时间" align="center" prop="endTime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.serviceEndTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="合同状态 " align="center" prop="state">-->
<!--        <template slot-scope="scope">-->
<!--          <div>-->
<!--            <el-tag v-if="scope.row.state === 0" style="cursor: pointer" :type=" 'info'">未开始</el-tag>-->
<!--            <el-tag v-if="scope.row.state === 1" style="cursor: pointer" :type=" 'success' ">已开始</el-tag>-->
<!--            <el-tag v-if="scope.row.state === 2" style="cursor: pointer" :type=" 'danger' ">已过期</el-tag>-->
<!--          </div>-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['module:maintenanceContract:edit']">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['module:maintenanceContract:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0"
                :total="total"
                :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize"
                @pagination="getList" />

    <!-- 添加或修改合同列对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="合同标题" prop="contractTitle">
          <el-input v-model="form.contractName" placeholder="请输入合同标题" size="mini"/>
        </el-form-item>
<!--        <el-form-item label="合同编号" prop="contractNo">-->
<!--          <el-input v-model="form.contractNo" placeholder="请输入合同编号" size="mini"/>-->
<!--        </el-form-item>-->
        <el-form-item label="合同文件">
          <fileUpload v-model="form.filePath"/>
        </el-form-item>
        <el-form-item label="计划时间" prop="dateRange">
          <el-date-picker v-model="form.dateRange" size="mini" style="width: 240px" value-format="yyyy-MM-dd" type="daterange"
                          :picker-options="time_option" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"/>
        </el-form-item>
<!--        <el-form-item label="合同状态 " prop="state">-->
<!--          <el-radio-group v-model="form.state" size="mini">-->
<!--            <el-radio :value="0" :label="0">未开始</el-radio>-->
<!--            <el-radio :value="1" :label="1">已开始</el-radio>-->
<!--            <el-radio :value="2" :label="2">已过期</el-radio>-->
<!--          </el-radio-group>-->
<!--        </el-form-item>-->
<!--        <el-form-item label="添加时间" prop="createTime">-->
<!--          <el-date-picker clearable size="mini" v-model="form.createTime" type="datetime" value-format="yyyy-MM-dd HH:mm:ss"-->
<!--                          placeholder="选择添加时间">-->
<!--          </el-date-picker>-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" size="mini" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel" size="mini">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listContract, getContract, delContract, addContract, updateContract, exportContract } from "@/api/module/event/maintenance/contract";
import {listPropertyNoPage} from "@/api/module/property/property";
import {listAllEstate} from "@/api/module/estate/estate";

export default {
  name: "Contract",
  data() {
    return {
      time_option: {
        disabledDate(time) {
          return time.getTime() < Date.now() -8.64e7;
        },
      },
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 导出遮罩层
      exportLoading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 合同列表格数据
      contractList: [],
      propertyListOptions:[],
      estatesOptions:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        maintenanceId:undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.queryParams.maintenanceId=this.$route.query.maintenanceId?this.$route.query.maintenanceId:undefined;
    this.getList();
    this.getEstateList();
    this.getPropertyList();
  },
  methods: {
    /** 查询合同列列表 */
    getList() {
      this.loading = true;
      listContract(this.queryParams).then(response => {
        this.contractList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        propertyId: undefined,
        estateId: undefined,
        contractTitle: undefined,
        contractNo: undefined,
        contractType: undefined,
        filePath: undefined,
        state: undefined,
        createTime: undefined,
        contractName:undefined,
        serviceBeginTime:undefined,
        serviceEndTime:undefined,
      }
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    getPropertyList() {
      listPropertyNoPage().then(response => {
        this.propertyListOptions = response.data;
      });
    },
    selectEstate(val){
      this.queryParamsEstate.propertyId = val;
      this.getEstateList();
    },
    getEstateList() {
      listAllEstate(this.queryParamsEstate).then(response => {
        console.log('getEstateList',response.data);
        this.estatesOptions = response.data;
      });
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length!==1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.form.maintenanceId=this.$route.query.maintenanceId?this.$route.query.maintenanceId:undefined;
      this.open = true;
      this.title = "添加合同列";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      this.form.maintenanceId=this.$route.query.maintenanceId?this.$route.query.maintenanceId:undefined;
      const id = row.id || this.ids;
      getContract(id).then(response => {
        this.loading = false;
        this.form = response.data;
        // this.form.dateRange= [this.form.serviceBeginTime,this.form.serviceEndTime];
        // this.form.dateRange[1]= ;
        this.open = true;
        this.title = "修改合同列";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if(this.form.dateRange.length === 2){
            this.form.serviceBeginTime = this.form.dateRange[0]+" 00:00:00";
            this.form.serviceEndTime = this.form.dateRange[1]+" 00:00:00";
          }
          if (this.form.id != null) {
            updateContract(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addContract(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除合同列编号为"' + ids + '"的数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.loading = true;
        return delContract(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有合同列数据项?', "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        this.exportLoading = true;
        return exportContract(queryParams);
      }).then(response => {
        this.download(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
};
</script>
